// 网关配置的前缀
var blogServer = "/blogSer"
var blogId = "";

let Authorization = "";
let refreshToken = "";

let readerId = "";
let readerName = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        blogServer = data.blogServer;
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")

    readerId = $("#readerId").html();
    readerName = $("#readerName").html();
    blogId = $("#blogId").val();
    getBlogById(blogId);
    getComment(blogId);
}

// 查询博客信息
function getBlogById(blogId) {
    $.ajax({
        type: "GET",
        async: false,
        url: blogServer + "/blog/oneBlog?blogId=" + blogId,
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(1000, data.resp_msg, function () {
                });
                $("#blogTitle").text(data.datas.blogTitle);
                $("#userId").text(data.datas.userId);
                $("#userName").text(data.datas.userName);
                $("#readNumber").text(data.datas.readNumber);
                $("#updateTime").text(data.datas.updateTime);

                var content = data.datas.blogContent;
                if (content === null || content === "") {
                    content = "暂无内容";
                }
                $("#blogContent").html(content);
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

// 查询所有评论信息
function getComment(blogId) {
    $.ajax({
        type: "get",
        async: false,
        url: blogServer + "/blog/commentInfo?blogId=" + blogId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            //清空数据
            $("#commentInfo").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {

                $("#commentInfo").append(
                    "<div class='post-author-info mt-20'>" +
                    "<div class='reviews-info' id='comment-" + i + "'>" +
                    "</div>" +
                    "</div>"
                );


                $.each(item, function (j, comment) {
                    var elementId = "item" + i + "-com" + j;
                    if (j === 0) {
                        $("#comment-" + i).append(
                            "<div class='author-info' id='" + elementId + "'>" +
                            "<h2 class='author-name'><a>" + comment.userName + "</a></h2>" +
                            "<p>" + comment.content + "</p>" +
                            "<div class='reply'>" +
                            "<span>" + comment.createDate + "</span>" +
                            "<a onclick='review(\"" + comment.commentId + "\"," + i + "," + j + ")'>回复</a>" +
                            "</div>" +
                            "</div>"
                        );
                    } else {
                        $("#comment-" + i).append(
                            "<div class='reviews-reply' id='" + elementId + "'>" +
                            "<h6 class='author-name'><a>" + comment.userName + "</a></h6>" +
                            "<p>" + comment.content + "</p>" +
                            "<div class='reply'>" +
                            "<span>" + comment.createDate + "</span>" +
                            "<a onclick='review(\"" + comment.commentId + "\"," + i + "," + j + ")'>回复</a>" +
                            "</div>" +
                            "</div>"
                        );
                    }

                });

            });
        },
        error: function (err) {
            // 异常
            console.log(err);
        }

    });

}

// 生成回复评论的文字框
function review(commentId, i, j) {
    var elementId = "item" + i + "-com" + j;
    $("#" + elementId).append(
        "<div id='" + elementId + "-area'>" +
        "<div>" +
        "<textarea style='width: 100%' id='" + elementId + "-text' placeholder='Your Message'></textarea>" +
        "</div>" +
        "<div style='text-align: right'>" +
        "<button onclick='delArea(" + i + ", " + j + ")'>取消</button>" +
        "<button onclick='submitBlog(\"" + commentId + "\"," + i + "," + j + ")'>提交</button>" +
        "</div>" +
        "</div>"
    )
}

// 取消回复删除文字框
function delArea(i, j) {
    $("#" + "item" + i + "-com" + j + "-area").remove();
}

// 提交
function submitBlog(commentId, i, j) {
    var content = $("#" + "item" + i + "-com" + j + "-text").val();
    var blogId = $("#blogId").val();
    $.ajax({
        type: "POST",
        async: false,
        url: blogServer + "/blog/commentInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": readerId,
            "userName": readerName,
            "blogId": blogId,
            "content": content,
            "parentId": commentId
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg);
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg);
            }

            // 刷新页面
            getComment(blogId);
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
            });
        }
    })
}
